<template>
    <div class="app cloak" :class="isready==true?'clear_cloak':''" v-if="info!=null">
        <div class="player">
            <div class="audio" v-if="info.media_type==1">
                <img src="/static/images/player_btn.png" class="play_btn" v-if="isaudioplay==false"
                     @click="audio_play()"/>
                <div class="audio_play_time_length" v-if="isaudioplay">{{audio_play_time_length}}</div>
                <div class="audio_total_time_length" v-if="isaudioplay">{{audio_total_time_length}}</div>
                <div class="progress">
                    <progress :percent="audio_percent"/>
                </div>
            </div>
            <div class="video" v-if="info.media_type==2">
                <video v-if="video_url!='' " :src="video_url" :direction="info.video_direction"
                       poster="http://res.caocan.beiyii.xyz/res/article/5c0d00d059845.jpg?x-oss-process=image/resize,m_fill,h_420,w_750"></video>
            </div>
        </div>
        <div class="first_info">
            <div class="title_code">
                <div class="code">{{info.code}}</div>
                <div class="title">{{info.title}}</div>
            </div>
            <div class="btnlist">
                <button class="btn" open-type="share">
                    <img class="icon" src="/static/images/share.png"/>
                    <span class="text">分享</span>
                </button>
                <a :href="'/pages/signup/share/main?code='+info.code" class="btn">
                    <img class="icon" src="/static/images/poster.png"/>
                    <span class="text">海报</span>
                </a>
                <a @click="praise()" class="btn">
                    <img class="icon" src="/static/images/dz2.png" v-if="info.isgreat"/>
                    <img class="icon" src="/static/images/dz.png" v-else/>
                    <span class="text">点赞({{info.numgreat}})</span>
                </a>
            </div>
        </div>
        <div class="info">
            <div class="title">作品信息</div>
            <div class="content">
                <div class="line">
                    <div class="label">作品类型:</div>
                    <div class="text">{{info.type_str}}</div>
                </div>
                <div class="line">
                    <div class="label">录制类型:</div>
                    <div class="text">{{info.media_type_str}}</div>
                </div>
                <div class="line">
                    <div class="label">报名时间:</div>
                    <div class="text">{{info.datetime}}</div>
                </div>
                <div class="line">
                    <div class="label">所在赛区:</div>
                    <div class="text">{{info.first_area_name}}-{{info.second_area_name}}</div>
                </div>
            </div>
        </div>
        <div class="info">
            <div class="title">选手信息</div>
            <div class="content">
                <div class="line">
                    <div class="label">选手姓名:</div>
                    <div class="text">{{info.name}}</div>
                </div>
                <div class="line">
                    <div class="label">选手性别:</div>
                    <div class="text">{{info.sex_str}}</div>
                </div>
            </div>
        </div>
        <div class="bottom"><a href="/pages/signup/area_list/main" class="btn" open-type="switchTab">我要报名</a><a
                href="/pages/index/index/main" class="btn" open-type="switchTab">更多作品</a></div>

    </div>
</template>

<script>
    var that = null

    const innerAudioContext = wx.createInnerAudioContext()
    var timeer = null
    export default {
        data() {
            return this.datainit({
                isaudioplay: false,
                video_url: '',
                audio_play_time_length: "",
                audio_total_time_length: "",
                code: null,
                info: null,
                audio_percent: 0,
                audio: {},
            })
        },
        created() {
            that = this
            this.isready = true
        },
        onShow() {
        },
        mounted() {
            wx.setKeepScreenOn({
                keepScreenOn: true
            })
            that.code = that.get("code")
            that.check()
            innerAudioContext.autoplay = false
            innerAudioContext.onStop(() => {
                that.audio_play_time_length = ""
                that.audio_total_time_length = ""
                that.audio_percent = 0
                that.isaudioplay = false
            })
            innerAudioContext.onPlay(() => {
                that.audio_percent = 0
                that.audio_total_time_length = that.sectostr(innerAudioContext.duration)
            })
            innerAudioContext.onEnded(() => {
                that.audio_play_time_length = ""
                that.audio_total_time_length = ""
                that.audio_percent = 0
                that.isaudioplay = false
            })
            innerAudioContext.onError((res) => {
            })
            innerAudioContext.onTimeUpdate(function () {
                that.audio_play_time_length = that.sectostr(innerAudioContext.currentTime)
                that.audio_total_time_length = that.sectostr(innerAudioContext.duration)
                if (innerAudioContext.duration > 0) {
                    that.audio_percent = parseInt(innerAudioContext.currentTime / innerAudioContext.duration * 100)
                }
            })

        },
        onUnload(){
            if(that.info.media_type==1&&  that.isaudioplay==true){
                innerAudioContext.stop()
            }

        },
        onHide(){
            if(that.info.media_type==1 &&  that.isaudioplay==true){
                innerAudioContext.stop()
            }

        },
        methods: {
            init() {
                that.$post("api/writing/get", {code: that.code}, function (data) {
                    that.isaudioplay = false;
                    that.video_url = '';
                    that.audio_play_time_length = "";
                    that.audio_total_time_length = "";
//                    that.code = null;
                    that.info = null;
                    that.audio_percent = 0;
                    that.audio = {}
                    that.info = data
                    if (data.media_type == 2) {
                        that.$post("api/index/signurl", {url: data.video + ".1280.mp4"}, function (video_url) {
                            that.video_url = video_url
                        })
                    }

                })
            },
            praise(){
                that.$post("api/writing/great",{writing_code:that.code},function (data) {
                    that.toast(data);
                    that.init()
                })
            },
            audio_play() {
                innerAudioContext.src = that.resurl + that.info.audio
                innerAudioContext.play()
                that.isaudioplay = true
            }
        },
        onShareAppMessage(res) {
            return {
                title: that.info.title,
                path: '/page/writing/index/main?code=' + that.info.code
            }
        }
    }
</script>

<style scoped lang="less">
    .player {
        position: relative;
        width: 100%;
        height: 210px;

        .audio {
            position: relative;
            width: 100%;
            height: 100%;
            background-image: url("http://res.caocan.beiyii.xyz/res/article/5c0d00d059845.jpg?x-oss-process=image/resize,m_fill,h_420,w_750");
            background-size: 100% 100%;

            .play_btn {
                position: absolute;
                width: 80px;
                height: 80px;
                left: 50%;
                top: 40px;
                margin-left: -40px;
                display: block;
            }

            .progress {
                position: absolute;
                bottom: 10px;
                left: 60px;
                right: 60px;
            }

            .audio_play_time_length {
                position: absolute;
                bottom: 10px;
                left: 0px;
                width: 60px;
                color: #FFF;
                text-align: center;
            }

            .audio_total_time_length {
                position: absolute;
                bottom: 10px;
                right: 0px;
                width: 60px;
                color: #FFF;
                text-align: center;
            }
        }

        .video {
            position: relative;
            width: 100%;
            height: 100%;

            video {
                width: 100%;
                height: 100%;
            }
        }
    }

    .first_info {
        box-sizing: border-box;
        border-left: 10px solid #FFF;
        border-right: 10px solid #FFF;
        border-top: 10px solid #FFF;
        border-bottom: 10px solid #FFF;
        background: #FFF;
        position: relative;
        width: 100%;

        .title_code {
            line-height: 30px;
            margin-top: 10px;
            width: 260px;

            .code {
                display: inline-block;
                padding-left: 5px;
                padding-right: 5px;
                height: 30px;
                background: #F60;
                color: #FFF;
                border-radius: 5px;
            }

            .title {
                display: inline;
            }

        }

        .btnlist {
            position: absolute;
            display: flex;
            width: 150px;
            height: 50px;
            right: 0;
            top: 0;
            display: flex;
            .btn {
                flex: 1;
                .icon {
                    display: block;
                    width: 25px;
                    height: 25px;
                    margin: auto;
                }

                .text {
                    text-align: center;
                    display: block;
                    font-size: 14px;
                }
            }

        }
    }

    .info {
        box-sizing: border-box;
        border-left: 10px solid #FFF;
        border-right: 10px solid #FFF;
        border-top: 10px solid #FFF;
        border-bottom: 10px solid #FFF;
        background: #FFF;
        position: relative;
        width: 100%;
        margin-top: 10px;

        .title {
            position: relative;
            width: 100%;
            height: 40px;
            line-height: 40px;
            border-bottom: 1px solid #CCC;
            text-align: center;
        }

        .content {
            position: relative;

            .line {
                position: relative;
                width: 100%;
                height: 30px;
                line-height: 30px;
                display: flex;

                .label {
                    width: 80px;
                }
            }
        }
    }

    .app {
        padding-bottom: 40px;
    }

    .bottom {
        position: fixed;
        z-index: 999;
        display: flex;
        width: 100%;
        height: 40px;
        bottom: 0;

        .btn {
            position: relative;
            flex: 1;
            text-align: center;
            color: #FFF;
            line-height: 40px;
        }

        .btn:nth-child(1) {
            background: #ff6600;
        }

        .btn:nth-child(2) {
            background: #01ac68;
        }

    }
</style>

